<?php 

$db_server = 'localhost'; // name of the server
$db_user = 'demouser'; // database user name
$db_pass = '5u990r7b1zdbr007'; // user's password
$db_name = 'demouser'; // database name

$con = mysql_connect($db_server,$db_user,$db_pass);
// verify the connection
if (!$con) {
	die('Could not connect to the DB' . mysql_error());
}

$db = mysql_select_db($db_name,$con);
// verify if the database was selected
if (!$db) {
	die('Could not connect select the DB' . mysql_error());
}

// $fbid = 6135410699;
$fbid = $user_profile['id'];
$result = mysql_query("SELECT * FROM oauth_user WHERE fbid = {$fbid}", $con);

while($row = mysql_fetch_assoc($result)) {
	$data[] = $row; // pouplating the data
}

if (isset($data) && count($data) > 0 && count($data) == 1) {
	
}
else {
	$name = mysql_real_escape_string($user_profile['name']);
	$first_name = mysql_real_escape_string($user_profile['first_name']);
	$last_name = mysql_real_escape_string($user_profile['last_name']);
	$link = mysql_real_escape_string($user_profile['link']);

	$insert_query = "INSERT INTO {$db_name}.oauth_user
	(user_id, fbid, name, first_name, last_name, link)
	VALUES
	(NULL, '{$fbid}', '{$name}', '{$first_name}', '{$last_name}', '{$link}');";

	$insert_result = mysql_query($insert_query, $con);
}

mysql_close($con);
